//
//  HMVisitorView.swift
//  iOSWeibo12
//
//  Created by Admin on 16/9/11.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

// 按钮点击协议
// 需要继承NSObjectProtocol
protocol HMVisitorViewDelegate: NSObjectProtocol {
    // 点击注册
    func vistorViewRegister()
    
    // 点击登录
    func vistorViewLogin()
}

class HMVisitorView: UIView {
    
    // 按钮点击代理
    weak var vistorViewDelegate: HMVisitorViewDelegate?
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: - 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1)
        setupUI()
    }
    
    /// 旋转动画
    func startRotationAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        
        // 要加上这句,不然切换到其他tabBar或者退出到桌面在切回来动画就停止了
        animation.isRemovedOnCompletion = false
        
        iconView.layer.add(animation, forKey: "homeRotation")
    }
    
    /**
     不同页面设置不同信息
     默认为首页,首页不需要调用
     parameter imageName: 图标名称
     parameter message:   消息文字
     */
    func setupVisitorInfo(imageName: String, message: String) {
        iconView.image = UIImage(named: imageName)
        
        messageLabel.text = message
        messageLabel.sizeToFit()
        
        // 隐藏小房子
        homeView.isHidden = true
        
        // 遮盖放到最下面
        self.sendSubview(toBack: coverView)
    }
    
    // MARK: - 准备UI
    private func setupUI() {
        // 添加子控件
        addSubview(iconView)
        // 添加遮罩视图
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 设置布局
        
        // 使用自动约束一定要设置成false.
        iconView.translatesAutoresizingMaskIntoConstraints = false
        // 遮盖
        coverView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        // iconView X方向
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0))
        // iconView Y方向
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: -40))
        
        // X
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: iconView, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0))
        // Y
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: iconView, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0))
        
        // 房子
        // X方向
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: iconView, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0))
        // Y方向
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: iconView, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0))
        
        // 消息label. CenterX和iconView.CenterX重合.,宽度224
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: iconView, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0))
        // 顶部距离iconView底部16
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: iconView, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 16))
        // 如果不参照其他控件toItem填nil. attribute填: NSLayoutAttribute.NotAnAttribute
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 224))
        
        // 注册按钮
        // 顶部距离messageLabel 16
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: messageLabel, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 16))
        // 左边和messageLabel的左边重合
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: messageLabel, attribute: NSLayoutAttribute.left, multiplier: 1, constant: 0))
        // 宽度 100
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 100))
        // 高度 35
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 35))
        
        // 注册按钮
        // 顶部距离messageLabel 16
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: registerButton, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0))
        // 左边和messageLabel的左边重合
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.right, relatedBy: NSLayoutRelation.equal, toItem: messageLabel, attribute: NSLayoutAttribute.right, multiplier: 1, constant: 0))
        // 宽度 100
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: registerButton, attribute: NSLayoutAttribute.width, multiplier: 1, constant: 0))
        // 高度 35
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: registerButton, attribute: NSLayoutAttribute.height, multiplier: 1, constant: 0))
    }
    
    // MARK: - 点击事件
    func registerClick() {
        vistorViewDelegate?.vistorViewRegister()
    }
    
    func loginClick() {
        vistorViewDelegate?.vistorViewLogin()
    }

    //MARK: 控件懒加载
    /// 转轮
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    /// 遮罩视图, 不要写maskView, 因为View有一个maskView属性
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    /// 房子
    private lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    /// 文字标签
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "关注一些人,看看有上面惊喜"
        label.textColor = UIColor.darkGray
        label.numberOfLines = 0
        label.sizeToFit()
        return label
    }()
    
    /// 注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        
        button.setTitle("注册", for: UIControlState.normal)
        button.setTitleColor(UIColor.orange, for: UIControlState.normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: UIControlState.normal)
        
        // 注册点击事件
        button.addTarget(self, action: #selector(registerClick), for: UIControlEvents.touchUpInside)
        
        return button
    }()
    
    /// 登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        
        button.setTitle("登录", for: UIControlState.normal)
        button.setTitleColor(UIColor.black, for: UIControlState.normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: UIControlState.normal)
        
        // 登录点击事件
        button.addTarget(self, action: #selector(loginClick), for: UIControlEvents.touchUpInside)

        return button
    }()
}
